<html>
  <head>
    <style>
      html, body {
        margin: 0;
        width: 100%;
        height: 100%
      }
      body {
        display: flex;
        flex-direction: column;
        align-items: center;
      }
    </style>
  </head>
  <body>
    <h3>Upload a video to transcode to mp4 (x264) and play!</h3>
    <video id="output-video" controls></video><br/>
    <input type="file" id="uploader">
    <p id="message"></p>
    <script type="module">
        const worker = new Worker(new URL('./transcode.worker.js', import.meta.url).href);
        worker.onmessage = (event) => {
            const {data} = event;
            message.innerHTML = 'Complete transcoding';
            const video = document.getElementById('output-video');
            video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video/mp4' }));
        }
        worker.onerror = (error) => console.log(error);
        const transcode = async ({ target: { files } }) => {
            const message = document.getElementById('message');
            const [file] = files;

            let name = file.name.split('.');
            const inType = name.pop();
            name = name.join();
            const buffer = await file.arrayBuffer();
            const outType = 'mp4';

            worker.postMessage({name, inType, outType, buffer}, [buffer]);

            message.innerHTML = 'Start transcoding';
        }
        const elm = document.getElementById('uploader');
        elm.addEventListener('change', transcode);
    </script>
  </body>
</html>
